package biz.t8.blog.controller.admin;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import biz.t8.blog.constant.WebConst;
import biz.t8.blog.controller.BaseController;
import biz.t8.blog.exception.TipException;
import biz.t8.blog.modal.Bo.RestResponseBo;
import biz.t8.blog.modal.Vo.UserVo;
import biz.t8.blog.modal.Vo.UserVoExample;
import biz.t8.blog.service.IContentService;
import biz.t8.blog.service.ILogService;
import biz.t8.blog.service.IMetaService;
import biz.t8.blog.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

/**
 * Ceated by cnxk 2019-12-01
 */
@Controller
@RequestMapping("/admin/users")
@Transactional(rollbackFor = TipException.class)
public class UsersController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UsersController.class);

    @Resource
    private IContentService contentsService;
    @Resource
    private IUserService userService;

    @Resource
    private IMetaService metasService;

    @Resource
    private ILogService logService;
    
    /**
       * 用户列表
     * @param page
     * @param limit
     * @param request
     * @return
     */
    @GetMapping(value = "")
    public String index(@RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "limit", defaultValue = "15") int limit, HttpServletRequest request) {
        UserVoExample userVoExample = new UserVoExample();
        userVoExample.setOrderByClause("created desc");
        userVoExample.createCriteria().andUserTypeEqualTo(WebConst.REGISTER_USER);
        PageInfo<UserVo> contentsPaginator = userService.getUsersWithpage(userVoExample,page,limit);
        request.setAttribute("users", contentsPaginator);
        return "admin/user_list";
    }

    /**
    * 删除注册用户
    * @param mid
    * @return
    */
   @PostMapping(value = "delete")
   @ResponseBody
   @Transactional(rollbackFor = TipException.class)
   public RestResponseBo delete(@RequestParam int uid) {
       try {
    	   
    	   UserVo userVo =  userService.queryUserById(uid);
    	   if (userVo == null) {
    		   return RestResponseBo.fail("用户不存在!");
    	   }
    	   if (userVo.getUserType() == WebConst.ADMIN_USER) {
    		   return RestResponseBo.fail("不能删除系统管理员!");
    	   }
    	   userService.delete(uid);
       } catch (Exception e) {
           String msg = "删除失败";
           if (e instanceof TipException) {
               msg = e.getMessage();
           } else {
               LOGGER.error(msg, e);
           }
           return RestResponseBo.fail(msg);
       }
       return RestResponseBo.ok();
   }
  
}
